package com.study.quiz.controller;

import com.study.quiz.cache.TagCache;
import com.study.quiz.pojo.Question;
import com.study.quiz.pojo.User;
import com.study.quiz.service.QuestionService;
import com.study.quiz.utils.ErrorIndexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CJunGod
 * @create 2020-03-04 20:52
 **/
@Slf4j
@Controller
public class PublishController {

    @Resource
    private QuestionService questionService;

    @Resource
    private ErrorIndexUtil errorIndexUtil;


    @GetMapping("/publish")
    public String goPublish(ModelMap modelMap, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            modelMap.put("tags", TagCache.getTag());
            return "publish";
        }

        errorIndexUtil.errorIndex("用户未登录，请登录后重试");

        return "redirect:/";
    }

    @GetMapping("/publish/{id}")
    public String doPublish(@PathVariable(name = "id") Integer questionId,
                            HttpServletRequest request, ModelMap modelMap) {
        Question question = questionService.findByQuestionId(questionId);
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            errorIndexUtil.errorIndex("用户未登录，请登录后重试");
            return "redirect:/";
        } else if (user.getId() != question.getCreator()) {
            errorIndexUtil.errorIndex("该问题不是您所发布，无法修改问题内容");
            return "redirect:/";
        }


        modelMap.put("questionId", question.getId());
        modelMap.put("questionTitle", question.getQuestionTitle());
        modelMap.put("description", question.getDescription());
        modelMap.put("tag", question.getTag());
        modelMap.put("tags", TagCache.getTag());

        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(@RequestParam("questionTitle") String questionTitle,
                            @RequestParam("description") String description,
                            @RequestParam("tag") String tag,
                            @RequestParam(name = "questionId", defaultValue = "0") Integer questionId,
                            HttpServletRequest request,
                            ModelMap modelMap) {
        modelMap.put("questionTitle", questionTitle);
        modelMap.put("description", description);
        modelMap.put("tag", tag);

        if (questionTitle.isEmpty()) {
            modelMap.put("error", "标题不能为空");
            return "publish";
        }

        if (description.isEmpty()) {
            modelMap.put("error", "问题描述不能为空");
            return "publish";
        }

        if (tag.isEmpty()) {
            modelMap.put("error", "标签不能为空");
            return "publish";
        }

        User user = (User) request.getSession().getAttribute("user");

        if (user == null) {
            modelMap.put("error", "用户未登录");
            return "publish";
        }
        Question question = new Question();
        if (questionId > 0) {
            question.setId(questionId);
            question.setQuestionTitle(questionTitle);
            question.setDescription(description);
            question.setTag(tag);
            question.setGmtModified(System.currentTimeMillis());
            questionService.updateById(question);
            errorIndexUtil.errorIndex("问题修改成功");
        } else {
            question.setQuestionTitle(questionTitle);
            question.setDescription(description);
            question.setTag(tag);
            question.setCreator(user.getId());
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(System.currentTimeMillis());
            questionService.insert(question);
            errorIndexUtil.errorIndex("问题发布成功");
        }

        return "redirect:/";
    }
}
